package com.estudy.contorller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.estudy.entity.Course;
import com.estudy.entity.ResultVo;
import com.estudy.entity.StudyPlan;
import com.estudy.entity.User;
import com.estudy.service.CourseService;
import com.estudy.service.PlanService;
import com.estudy.service.UserService;
import com.google.common.collect.Maps;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author: yi.jianhua
 * @create: 2019/09/06 10:15
 */
@Controller
@RequestMapping("/plan")
public class PlanController {
    private static final Logger logger = Logger.getLogger(PlanController.class);
    @Autowired
    private CourseService courseService;
    @Autowired
    private PlanService planService;
    @Autowired
    private UserService userService;

    @RequestMapping("/addPage")
    public ModelAndView planCreatePage() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("plan/plan-add");
        List<Course> courseName = courseService.queryAllOnlineCourse();
        modelAndView.addObject("courses", courseName);
        return modelAndView;
    }

    @RequestMapping("/listPage")
    public ModelAndView planListPage() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("plan/plan-list");
        return modelAndView;
    }


    @RequestMapping("/save")
    @ResponseBody
    public String planAdd(HttpServletRequest resquest, String planName, String startTime, String endTime, String courses, String planDesc) {
        ResultVo resultVo = new ResultVo();
        try {
            User user = (User) resquest.getSession().getAttribute("u");
            if (user == null) {
                user = new User();
                user.setId("0");
            }
            planService.addPlan(planName, startTime, endTime, courses, planDesc, Integer.parseInt(user.getId()));
            resultVo.setStatus(200);
            resultVo.setMessage("SUCCESS");
        } catch (Exception e) {
            logger.error("planAdd error", e);
            resultVo.setStatus(500);
            resultVo.setMessage("保存失败");
        }
        return JSON.toJSONString(resultVo, SerializerFeature.WriteMapNullValue);
    }

    @RequestMapping("/dataList")
    @ResponseBody
    public String queryUserData() {
        try {
            Map<String, Object> map = Maps.newHashMap();
            List<User> users = userService.queryAllUsers();
            Map<String, String> userNameMap = convertUserMap(users);
            List<StudyPlan> plans = planService.queryAllPlans();
            for (StudyPlan s : plans) {
                s.setCreatUserName(userNameMap.get(String.valueOf(s.getUid())));
            }
            System.out.println(JSON.toJSONString(plans));
            map.put("data", plans);
            map.put("recordsTotal", plans.size());
            map.put("recordsFiltered", plans.size());
            map.put("pageNum", 1);
            return JSON.toJSONString(map, SerializerFeature.WriteMapNullValue);
        } catch (Exception e) {
            logger.error("queryUserData error", e);
        }
        return "";
    }

    private Map<String, String> convertUserMap(List<User> users) {
        Map<String, String> map = Maps.newHashMap();
        for (User user : users) {
            map.put(user.getId(), user.getNickName());
        }
        return map;
    }

    @RequestMapping("/updateStatus")
    @ResponseBody
    public String updatePlanStatus(String pid, int status) {
        ResultVo resultVo = new ResultVo();
        try {
            planService.updatePlanStatus(pid, status);
            resultVo.setStatus(200);
            resultVo.setMessage("SUCCESS");
        } catch (Exception e) {
            logger.error("updatePlanStatus error", e);
            resultVo.setStatus(500);
            resultVo.setMessage("更新失败");
        }
        return JSON.toJSONString(resultVo, SerializerFeature.WriteMapNullValue);
    }

}
